package com.xms.security.service.sitemap;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.event.core.IEventPublisher;
import com.xms.security.entity.Privilege;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * PrivilegeDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/10/2
 */
@CacheConfig(cacheNames = "privilege", keyGenerator = "CacheKeyGenerator")
@Service
public class PrivilegeDeleterService extends DeleterService<Privilege> implements IPrivilegeDeleterService {
    private IEventPublisher _eventPublisher;

    @Autowired
    public PrivilegeDeleterService(IEventPublisher eventPublisher) {
        _eventPublisher = eventPublisher;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean delete(Privilege entity) throws XmsDependencyException, SQLException {
        boolean success = super.deleteById(entity.getPrivilegeid());
        if (success) {
            //权限更改事件
            _eventPublisher.publish(new AuthorizationStateChangedEvent(SiteMapDefaults.MODULE_NAME, Collections.singletonList(entity.getPrivilegeid()), false));
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        Privilege entity = super.getById(id);
        if (entity != null) {
            return this.delete(entity);
        }
        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<Privilege> queryWrapper) throws SQLException, XmsDependencyException {
        List<Privilege> entityList = super.list(queryWrapper);
        if (CollectionUtil.notEmpty(entityList)) {
            for (Privilege entity : entityList) {
                this.delete(entity);
            }
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws SQLException, XmsDependencyException {
        if (CollectionUtil.notEmpty(idList)) {
            for (UUID id : idList) {
                this.deleteById(id);
            }
        }
        return true;
    }
}
